﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using ContractManager.Common.Class;
using ContractManager.Common.FormBase;
using ContractManager.BLL.Business;
using ContractManager.Lists.Contracts;
using DevExpress.XtraEditors;
using Stimulsoft.Report;
using Stimulsoft.Report.Components;

namespace ContractManager.Reports.Forms
{
    public partial class FrmBusinessReport : XtraFormBase
    {
        #region "Variable"

        private Entity.Forms _form = new Entity.Forms();
        private bool _allowEdit;
        private bool _onlyView;
        private bool _allowDelete;
        public List<DateTime> ListLockDate { get; set; }

        private DateTime FirstYear;
        private DateTime FromDate;
        private DateTime ToDate;

        #endregion

        #region "Form Construction"
        public FrmBusinessReport()
        {
            InitializeComponent();
        }
        #endregion

        #region "Private Method"

        private void CheckPer(int formNo)
        {
            var item = ClsForms.SelectByUserForm(ClsVariable.RoleNo, formNo);
            if (item == null) return;

            _allowEdit = item.AllowEdit;
            _allowDelete = item.AllowDelete;
            _form = ClsForms.SelectByPk(formNo);
            this.ListLockDate = ClsVoucherLock.GetLockByFormNo(formNo);
        }

        private void BlankForm()
        {
            txtFromValue.EditValue = null;
            txtYear.EditValue = ClsVariable.Year;
            txtType.SelectedIndex = 0;
        }

        private void DoPerform()
        {
            string fileName = ReportName;
            string reportName = ClsVariable.GetReportFullPath(fileName);
            
            var report = new Stimulsoft.Report.StiReport();
            report.Load(reportName);

            ClsVariable.CreateWaitDialog(ClsVariable.StrCaption, "Vui lòng đợi...");
            ClsVariable.SetWaitDialogCaption("Đang nạp dữ liệu báo cáo...");

            DataSet dsReport = ClsReports.GetBusinessReport(txtYear.Value, txtFromValue.Value, txtType.SelectedIndex + 1, this.StoreProcedureName);

            report.RegData("DeploymentJob",dsReport.Tables[0]);
            report.RegData("DocumentProgress", dsReport.Tables[1]);
            report.RegData("ProductBusinessActivity", dsReport.Tables[2]);
            report.RegData("PromotionBid", dsReport.Tables[3]);
            report.Compile();
           // report.CompiledReport.Click += click;
            ClsVariable.CloseWaitDialog();
            // In thông tin đơn vị, địa chỉ
            string strKyBaoCao = txtType.SelectedIndex == 0
                              ? String.Format("Tuần {0} năm {1}", txtFromValue.Value, txtYear.Value)
                              : (txtType.SelectedIndex == 1
                                     ? String.Format("Tháng {0} năm {1}", txtFromValue.Value, txtYear.Value)
                                     : (txtType.SelectedIndex == 2
                                            ? String.Format("Quý {0} năm {1}", txtFromValue.Value, txtYear.Value)
                                            : String.Format("Năm {0}",txtYear.Value)));
            report["KyBaoCao"] = strKyBaoCao;

            ClsVariable.ApplyReportParam(report, ClsVariable.DtParam);
            report.Show(this.MdiParent);
            
        }

        

        
        

        #endregion

        #region "Form Event"
        private void FrmBusinessReportLoad(object sender, EventArgs e)
        {
            BlankForm();
        }
        #endregion


        #region "Button Event"
        private void btnOk_Click(object sender, EventArgs e)
        {
            DoPerform();

        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        #endregion

        #region "Lookup Event"
        private void txtYear_EditValueChanged(object sender, EventArgs e)
        {
            if (txtYear.Value < 1900) return;
            //If week
            switch (txtType.SelectedIndex)
            {
                case 0://Week
                    {
                        txtFromValue.Properties.MinValue = 1;
                        txtFromValue.Properties.MaxValue = ClsDateUtils.WeeksInYear(Convert.ToInt32(txtYear.Value)); 
                    }
                    break;
                case 1://Month
                    txtFromValue.Properties.MinValue = 1;
                    txtFromValue.Properties.MaxValue = 12;
                    break;
                case 2://Year
                    txtFromValue.Properties.MinValue = 1;
                    txtFromValue.Properties.MaxValue = 4;
                    break;
            }
        }

        private void txtType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtYear.Value < 1900) return;
            //If week
            switch (txtType.SelectedIndex)
            {
                case 0://Week
                    {
                        txtFromValue.Properties.MinValue = 1;
                        txtFromValue.Properties.MaxValue = ClsDateUtils.WeeksInYear(Convert.ToInt32(txtYear.Value));
                    }
                    break;
                case 1://Month
                    txtFromValue.Properties.MinValue = 1;
                    txtFromValue.Properties.MaxValue = 12;
                    break;
                case 2://Year
                    txtFromValue.Properties.MinValue = 1;
                    txtFromValue.Properties.MaxValue = 4;
                    break;
            }
        }
        #endregion

        


    }
}
